<?php
/**
 * LeaveMsgModel.class.php
 * 留言模型
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-04-27
 */

namespace Admin\Model;

class LeaveMsgModel extends BaseModel {

	protected 	$trueTableName 	= 't_leave_msg';
	public		$errMsg 	= false;

	/**
	 * 获取留言列表
	 * @param 	$relateType 	留言类型
	 * @param 	$relateId 	关联ID
	 * @param 	$status 	状态
	 * @param 	$page 	页码数
	 * @param 	&$pageHtml 	页码HTMl
	 * @return 	$list
	 */
	function getList($relateType, $relateId, $status, $page, &$pageHtml){
		if($status !== false)
			$where 	= 'a.status = '.$status;
		else
			$where 	= 'a.status != -1';

		if($relateType !== false)
			$where 	.= ' and a.relate_type = '.$relateType;
		else
			$where 	.= ' and a.relate_type != 9';
		if($relateId !== false)
			$where 	.= ' and a.relate_id = '.$relateId;

		$list 	= $this->table('t_leave_msg as a')
			->join('left join t_user b on a.user_id = b.id')
			->join('left join t_leave_msg c on c.relate_type = 9 and c.relate_id = a.id')
			->where($where)
			->order('id desc')
			->page($page, C('PAGE_COUNT'))
			->field('a.id, a.user_id, a.content, a.relate_type, a.relate_id, a.show_place, 
				a.create_time, a.status, b.nickname, b.headimgurl, 
				c.content as reply_cont, c.create_time as reply_time')
			->select();
		$list 	= $this->_analyField('leavemsg', $list, true);

		foreach($list as $item){
			//关联课程ID
			if($item['relate_type'] == 1)
				$relateCourseIds[] 		= $item['relate_id'];
			//关联活动ID
			if($item['relate_type'] == 2)
				$relateActivityIds[]	= $item['relate_id'];
		}

		//关联课程获取
		if($relateCourseIds){
			$relateCourses 	= M('t_course', null)
				->where('id in (%s)', implode(',', $relateCourseIds))
				->getField('id, name as title, status');
		}	
		//关联活动获取
		if($relateActivityIds){
			$relateActivities 	= M('t_activity', null)
				->where('id in (%s)', implode(',', $relateActivityIds))
				->getField('id, title, status');
		}
		
		if($pageHtml === true){
			$count 	= $this->table('t_leave_msg as a')->where($where)->count();
			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		foreach($list as &$item){
			if($item['relate_type'] == 1)
				$item['relate_info']	= $relateCourses[$item['relate_id']];
			if($item['relate_type'] == 2)
				$item['relate_info']	= $relateActivities[$item['relate_id']];
		}

		return $list;
	}

	/**
	 * 更新留言信息
	 * @param 	$msgId
	 * @param 	$msgInfo
	 * @return 	boolean
	 */
	function updateItem($msgId, $msgInfo){
		$result 	= $this->where('id = %d', $msgId)->save($msgInfo);
		if(!$result){
			$this->errMsg 	= '留言信息更新失败!';
			return false;
		}
		return true;
	}

	/**
	 * 回复留言
	 * @param 	$msgId
	 * @param 	$content
	 * @return 	$msgInfo
	 */
	function replyMsg($msgId, $content){
		$msgInfo 	= [
			'user_id'	=> 0,
			'content'	=> $content,
			'relate_type'	=> 9,
			'relate_id'	=> $msgId,
			'create_time'	=> time(),
			'status'	=> 1
		];

		//删除原回复
		$this->where('relate_type = %d and relate_id = %d', 9, $msgId)->delete();
		//添加新回复
		$msgId 	= $this->add($msgInfo);
		if(!$msgId){
			$this->errMsg 	= '回复留言失败!';
			return false;
		}

		$msgInfo['id']	= $msgId;
		return $msgInfo;
	}
}